import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		titOn: 'all',
		pageIndex: 1,
		pageItems: 10,
		totalPages: 0,
		status: 0,
		//任务列表
		examList: []
	},
	handleTap1(event) {   //考试进度筛选
		var sessType = event.target.id;
		var going;
		var pass;
		var abence;
		if (sessType == "going") {     //进行中
			going = "goingShow";
			pass = "passHide";
			abence = "abenceHide";
		} else if (sessType == "pass") {  //已参加
			pass = "passShow";
			going = "goingHide";
			abence = "abenceHide";
		} else if (sessType == "abence") {  //未参加
			abence = "abenceShow";
			going = "goingHide";
			pass = "passHide";
		} else if (sessType == "all") {  //已参加
			abence = "abenceShow";
			going = "goingShow";
			pass = "passShow";
		}
		this.setData({
			titOn: sessType,
			going: going,
			pass: pass,
			abence: abence

		})
	}, 
	onLoad() {
		//查询我的考试任务列表
		this.loadExamList();
	},

	//查询我的考试任务列表
	loadExamList: function(e) {
		var status = 0;
		if ("string" == typeof (e)) {
			status = e;
		} else {
			if (e != undefined) {
				status = e.currentTarget.dataset.code;  //表示从页面上点击过来de,是一个对象
				this.handleTap1(e)
				this.setData({
					"pageIndex": 1,
					"status": status
				})
			} else {
				status = this.data.status;    //表示从页面上拉加载的 直接获取
			}
		}
		var newUrl = getApp().globalData.dangjianUrl + "/exam/examList";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["status"] = status + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "查询我的考试任务列表接口调用失败").then(
			(respData) => {
				console.log(respData)
				var result = respData.data.result.code;
				if (1 == result) {
					var exams = respData.data.data;
					if (null != exams && undefined != exams) {
						var totalPages = exams.totalPage;
						var pageIndex = exams.pageNumber;
						var items = exams.resultSize;
						var examList = [];
						for (var index in exams.results) {
							var exam = exams.results[index];
							var json = {};
							exam.CREATE_TIME;
							var status;
							//获取开始时间戳
							var start = exam.startTime;
							start = start.substring(0, 19);
							start = start.replace(/-/g, '/');
							var getStartTime = new Date(start).getTime();
							//获取结束时间戳
							var end = exam.endTime;
							end = end.substring(0, 19);
							end = end.replace(/-/g, '/');
							var getEndTime = new Date(end).getTime();
							// 系统时间戳
							var sysTimeStamp = Date.parse(new Date())
							if (sysTimeStamp <= getStartTime) {
								status = 1;	//未开始
							} else if (sysTimeStamp >= getStartTime && sysTimeStamp <= getEndTime) {
								status = 2; //进行中
							} else {
								status = 3;	//已结束
							}
							json["status"] = status;
							json["startTime"] = exam.startTime;
							json["endTime"] = exam.endTime;
							json["examId"] = exam.id;			//试卷ID
							json["timeLimit"] = exam.timeLimit;
							json["paperName"] = exam.paperName;
							json["fullScore"] = exam.fullScore;
							json["passScore"] = exam.passScore;
							json["introduction"] = exam.introduction;
							json["checks"] = exam.checks;
							json["radio"] = exam.radio;
							json["judge"] = exam.judge;
							json["topicNum"] = exam.topicNum;
							json["credit"] = exam.credit;

							json["joinNum"] = exam.joinNum;
							json["scoreNum"] = exam.scoreNum;	//用户历史最高分
							json["userMaxScore"] = exam.userMaxScore;
							examList.push(json);
						}
					}
					if (e == undefined) {
						var oldexamList = this.data.examList;
						examList = oldexamList.concat(examList);          //表示从页面上拉加载的 直接获取
					}
					this.setData({
						"examList": examList,
						"totalPages": totalPages,
						"pageIndex": pageIndex,
						"pageitems": items,
					});
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	//开始考试详情
	examinationPage(event) {
		var examId = event.currentTarget.dataset.examid;
		var timeLimit = event.currentTarget.dataset.timelimit;
		var status = event.currentTarget.dataset.status;
		if (status == 1) {
			this.showPageMsg("tips", "考试暂未开始，请重新选择！");
		}
		if (status == 2) {
			wx.navigateTo({
				url: '../examination/examination?examId=' + examId + '&timeLimit=' + timeLimit
			})
		}
		if (status == 3) {
			wx.showToast({
        title: '考试暂已结束，请重新选择！',
      })
		}

	},



	// 页面被拉到底部
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		var folderCode = this.data.currentFolderCode;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex,
				"folderCode": folderCode,
			});
			this.loadExamList();
		} else {
			this.showPageMsg("tips", "没有更多内容了");
		}
	},

	// 页面 上拉刷新
	onPullDownRefresh: function(e) {
		this.setData({
			pageIndex: 1,
			contentList: [],
		});

		this.onLoad();
		wx.stopPullDownRefresh();
	},


});
